//
//  News.m
//  555彩票
//
//  Created by zhoubo on 2017/7/4.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "News.h"
#import "NetWorkTool.h"
#import "BmobSDK/Bmob.h"

@implementation News

+(instancetype)newsWithDict:(NSDictionary *)dic
{
    News* new = [[self alloc]init];
    [new setValuesForKeysWithDictionary:dic];
    
    return new;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}

+(instancetype)newsWithBmobObj:(BmobObject*)obj
{
    News* new = [[self alloc]init];
    new.title = [obj objectForKey:@"title"];
    new.content = [obj objectForKey:@"content"];
    new.date = [obj objectForKey:@"date"];
    new.comment = [obj objectForKey:@"comment"];
    new.imgurls = [obj objectForKey:@"imgurls"];
    new.Id = [obj objectForKey:@"id"];
    new.author = [obj objectForKey:@"author"];
    
    return new;
}

+(void)loadNewsWithName:(NSString *)name success:(void (^)(NSArray *))success faild:(void (^)(NSError *))failed
{
    BmobQuery* query = [BmobQuery queryWithClassName:name];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            failed(error);
        }
        else
        {
            NSMutableArray* arr = [[NSMutableArray alloc]initWithCapacity:array.count];
            
            for (BmobObject* obj in array) {
                News* news = [News newsWithBmobObj:obj] ;
                [arr addObject:news];
            }
            
            success(arr.copy);
        }
        
        
        
    }];
}

+(instancetype)newsFromLoopModel:(LoopModel*)loopmodel
{
    News*news = [[News alloc]init];
    news.title = loopmodel.title;
    news.content = loopmodel.content;
    if (loopmodel.detailimgs.count) {
        news.imgurls = loopmodel.detailimgs;
    }
    else
    {
        news.imgurls = [NSArray arrayWithObject:loopmodel.image];
    }
    return news;
}


@end
